/**
 * <p>功能:系统管理模块-资源管理-数据字典 <p>
 * @extends Ext.tab.Panel #e8edf0
 * @author wys
 */
Ext.define('Jason.view.protal.module.systemManager.sources.dict.MainView', {
    extend: 'Ext.container.Container',
    style: 'background:#f1f4f8;',
    xtype: 'sysDictMainView',
    requires: [
        'Ext.panel.Panel',
        'Ext.tree.Panel',
        'Wys.form.field.SearchField',
        'Ext.button.Button',
        'Jason.store.protal.module.systemManager.sources.dict.DictStore',
        'Jason.view.protal.module.systemManager.sources.dict.ViewModel',
        'Jason.view.protal.module.systemManager.sources.dict.MainCtrl'
    ],
    controller: 'sources_dict_MainViewCtrl',
    viewModel: { type: 'sources_dict_MainViewModel' },
    layout: {
        type: 'hbox',
        align: 'stretch'
    },
    items: [
        {
            cls: 'faq-left-sidebar shadow-panel',
            iconCls: "x-fa fa-th-list",
            title: '字典类别',
            border: true,
            tools: [{
                type: 'plus',
                listeners: { 'click': 'addDictParent' }
            }], split: false,
            autoScroll: true,
            xtype: 'treepanel',
            ui: 'light',
            width: 230,
            moreRightMenu: [{
                iconCls: 'fa fa-edit',
                text: '修改',
                handler: function () {
                    var me = this,
                        view = Ext.getCmp(me.viewId),
                        record = view.getSelection()[0];
                    EUtil.RS({
                        url: reqUrl.sys.dict.getOne,
                        method: 'GET',
                        params: {
                            code: record.data.id
                        },
                        callback: function (resp) {
                            var newrecord = Ext.create('Jason.model.protal.module.systemManager.souces.dict.DictModel', resp);
                            Ext.create('Wys.form.BaseWindow', {
                                width: 524,
                                listeners: {
                                    show: function (win) {
                                        win.down('baseForm').loadRecord(newrecord);
                                    }
                                },
                                title: '修改字典类别【' + newrecord.data.key + '】',
                                iconCls: 'fa fa-edit',
                                modal: true,
                                items: [{
                                    xtype: 'baseForm',
                                    items: [{
                                        xtype: 'fieldset',
                                        title: '基本信息',
                                        collapsible: false,
                                        defaultType: 'textfield',
                                        layout: {
                                            type: 'table',
                                            columns: 1
                                        },
                                        defaults: {
                                            width: '100%',
                                            labelWidth: 100
                                        },
                                        items: [
                                            { name: 'id', xtype: 'hiddenfield' },
                                            { fieldLabel: '类别名称', name: 'key', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required },
                                            {
                                                fieldLabel: '类别代码', name: 'type', xtype: 'textfield', flex: 1, anchor: '100%', allowBlank: false, afterLabelTextTpl: cfg.required,
                                                emptyText: '用于查询时快速检索'
                                            },
                                            { fieldLabel: '父类', xtype: 'hiddenfield', name: 'parent', value: -1 },
                                            { fieldLabel: '状态', xtype: 'hiddenfield', name: 'state', value: 1 },
                                            {
                                                xtype: 'radiogroup',
                                                fieldLabel: '状态',
                                                vertical: true,
                                                simpleValue: true,
                                                bind: '{state}',
                                                layout: 'hbox',
                                                items: [
                                                    {
                                                        boxLabel: '启用',
                                                        checked: true,
                                                        name: 'state',
                                                        inputValue: 1
                                                    }, {
                                                        boxLabel: '停用',
                                                        name: 'state',
                                                        inputValue: 2
                                                    }
                                                ]
                                            },
                                            { fieldLabel: '备注', name: 'remarks', flex: 1, xtype: 'textareafield', grow: true, anchor: '100%' }
                                        ]
                                    }],
                                    buttons: [{
                                        text: '保存',
                                        iconCls: 'x-fa fa-floppy-o',
                                        handler: function (button) {
                                            var form = button.up('baseForm').getForm();
                                            formValues = form.getValues();
                                            EUtil.RS({
                                                url: reqUrl.sys.dict.update,
                                                params: formValues,
                                                callback: function (resp) {
                                                    if (resp.success) {
                                                        EUtil.showSuccess(I18N.SavedAndNothing);
                                                        button.up("window").close();
                                                    } else {
                                                        EUtil.showError(Ext.String.format(I18N.FailedOtherCode, resp.code, resp.msg));
                                                    }
                                                }
                                            });
                                        }
                                    }, {
                                        text: '关闭',
                                        itemId: 'close',
                                        iconCls: 'x-fa fa-close',
                                        handler: function (button) {
                                            this.up("window").close();
                                        }
                                    }]
                                }]
                            }).show();
                        }
                    });
                }
            }, {
                iconCls: 'x-fa fa-trash',
                text: '删除',
                handler: function () {
                    var me = this,
                        view = Ext.getCmp(me.viewId),
                        viewStore = view.getStore(),
                        record = view.getSelection()[0];
                    EUtil.showMsg({
                        option: 3,
                        title: '温馨提示',
                        message: '您确定删除当前选中字典类型为<span style="color:red;">『' + record.data.text + '』</span>吗？',
                        callback: function (btn) {
                            if (btn === 'ok') {
                                EUtil.RS({
                                    url: reqUrl.sys.dict.removeParent,
                                    method: 'POST',
                                    params: {
                                        code: record.data.id
                                    },
                                    callback: function (resp) {
                                        if (resp.success) {
                                            viewStore.load();
                                        } else {
                                            EUtil.showError(Ext.String.format(I18N.FailedOtherCode, resp.code, resp.msg));
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }],
            useArrows: true,
            collapsible: false,
            collapsed: false,
            rootVisible: false,
            listeners: { itemclick: 'onClickDictParentTreeItem' },
            itemId: 'dictListTree',
            bind: '{dictTreeStore}'
        },
        {
            xtype: 'baseView',
            ui: 'light',
            itemId: 'dictMainView',
            margin: '0 0 0 6',
            flex: 1,
            bind: '{souresDictStore}',
            enabledMark: false,
            dockedItems: [{
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                    xtype: 'gridsearchfield',
                    paramName: 'key'
                }, {
                    xtype: 'segmentedbutton',
                    items: [{
                        xtype: 'previewbutton',
                        rightCode: 'DICTMANAGER'
                    }, {
                        xtype: 'newbutton',
                        rightCode: 'DICTMANAGER'
                    }, {
                        xtype: 'editbutton',
                        rightCode: 'DICTMANAGER'
                    }, {
                        xtype: 'deletebutton',
                        rightCode: 'DICTMANAGER'
                    }]
                }, '->', {
                    xtype: 'refresh',
                    rightCode: 'DICTMANAGER',
                    viewItemId: '#dictMainView'
                }]
            }, {
                xtype: 'pagingtoolbar',
                enabledMoreOpt: true,
                isShow: false,
                bind: '{souresDictStore}',
                dock: 'bottom'
            }],
            columns: [
                { xtype: 'rownumberer', text: '序号', align: 'center', width: 50 },
                {
                    text: '字典类型', dataIndex: 'typename', flex: .5, renderer: function (v, m) {
                        m.style = "font-weight:bold";
                        return v;
                    }
                },
                { text: '数据名称', dataIndex: 'key', flex: .5, menuDisabled: true },
                { text: '数据值', dataIndex: 'value', width: 70, menuDisabled: true, align: 'center' },
                {
                    text: '状态', dataIndex: 'state', width: 50, menuDisabled: true,
                    renderer: function (v) {
                        return v == 1 ? "正常" : "删除";
                    }
                },
                { text: '权重', dataIndex: 'order', align: 'center', width: 70, menuDisabled: true, xtype: 'numbercolumn', format: '0' },
                { text: '创建日期', dataIndex: 'createDate', width: 135, xtype: 'datecolumn', format: 'Y-m-d H:i' },
                {
                    xtype: 'command',
                    text: '操作',
                    align: 'center',
                    width: 95,
                    commands: [
                        { command: "preview", text: '<i class="fa fa-search"></i>', tooltip: { text: '预览' }, cls: 'radiusBtn command-info-color' },
                        //					   { command: "addChildren",text: "<i class='fa fa-plus'></i>",tooltip :{text:'添加子集'},cls:'radiusBtn command-success-color'},
                        { command: "modify", text: "<i class='fa fa-edit'></i>", tooltip: { text: '编辑' }, cls: 'radiusBtn command-modify-color' },
                        { command: "remove", text: "<i class='fa fa-trash'></i>", tooltip: { text: '删除' }, cls: 'radiusBtn command-remove-color' }
                    ],
                    listeners: {
                        prepareCommands: 'onGridRepareCommands',
                        command: 'onGridCommand'
                    }
                }
            ]
        }
    ]
});